package com.fw.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.ObjectMetadata;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Date;

public class OSSUtil {
    public static final String endPoint = "oss-cn-zhangjiakou.aliyuncs.com";
    public static final String accessKeyId = "LTAI4FzdgL59qZzG6mYgM199";
    public static final String accessKeySecret = "seutvT6rsaPAAF3SPqyawn7JkApSxk";
    public static final String bucketName = "jameswaterman1";

    public static OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);

    public static void upLoadFile(String name, MultipartFile file)
        throws IOException, OSSException{
        String url = "";
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(file.getSize());
        objectMetadata.setCacheControl("no-cache");
        objectMetadata.setHeader("Pragma", "no-cache");
        objectMetadata.setContentType(getcontentType(name.substring(name.lastIndexOf("."))));
        objectMetadata.setContentDisposition("inline;filename=" + name);

        ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);
        ossClient.putObject(bucketName, name, new ByteArrayInputStream(file.getBytes()));
        ossClient.shutdown();
        return;
    }

    public static void uploadAvatar(String userid, MultipartFile file) throws IOException {
        String name = userid + ".jpg";
        upLoadFile(name, file);
    }

    public static String uploadImage(MultipartFile file) throws IOException {
        String name = MyUtils.getUUIDWithoutLine() + ".jpg";
        upLoadFile(name, file);
        return name;
    }

    public static void uploadUserFile(MultipartFile file) throws IOException {
        String name = file.getName();
        upLoadFile(name, file);
    }

    public static String getDownLoadUrl(String filename){
        ossClient = new OSSClientBuilder().build(endPoint,accessKeyId,accessKeySecret);
        Date expiration = new Date(new Date().getTime() + 3600L * 1000 * 24 * 365 * 100);
        URL url = ossClient.generatePresignedUrl(bucketName, filename, expiration);
        ossClient.shutdown();
        return url.toString();
    }

    public static boolean isObjectExist(String filename){
        ossClient = new OSSClientBuilder().build(endPoint,accessKeyId,accessKeySecret);
        boolean found = ossClient.doesObjectExist(bucketName, filename);
        ossClient.shutdown();
        return found;
    }
    public static String getcontentType(String FilenameExtension) {
        if (FilenameExtension.equalsIgnoreCase(".bmp")) {
            return "image/bmp";
        }
        if (FilenameExtension.equalsIgnoreCase(".gif")) {
            return "image/gif";
        }
        if (FilenameExtension.equalsIgnoreCase(".jpeg") ||
                FilenameExtension.equalsIgnoreCase(".jpg") ||
                FilenameExtension.equalsIgnoreCase(".png")) {
            return "image/jpg";
        }
        if (FilenameExtension.equalsIgnoreCase(".html")) {
            return "text/html";
        }
        if (FilenameExtension.equalsIgnoreCase(".txt")) {
            return "text/plain";
        }
        if (FilenameExtension.equalsIgnoreCase(".vsd")) {
            return "application/vnd.visio";
        }
        if (FilenameExtension.equalsIgnoreCase(".pptx") ||
                FilenameExtension.equalsIgnoreCase(".ppt")) {
            return "application/vnd.ms-powerpoint";
        }
        if (FilenameExtension.equalsIgnoreCase(".docx") ||
                FilenameExtension.equalsIgnoreCase(".doc")) {
            return "application/msword";
        }
        if (FilenameExtension.equalsIgnoreCase(".xml")) {
            return "text/xml";
        }
        return "image/jpg";
    }

}
